import {parseTime, request} from "../../utils/util";

Page({
  data: {
    account_balance: '',
    give_balance: '',
    recharge_balance: '',
    list: [],
    page: 1,
    loadMore: true,
  },
  // 获取列表数据
  getList(page) {
    request({
      url: '/api/web/personal/recharge',
      params: {
        page
      },
      success: ({ code, data }) => {
        if (code === 200) {
          let currentData = data.data.map(item => {
            item.created_at_show = parseTime(item.created_at, '{y}/{m}/{d}　{h}:{i}')
            return item
          })
          if (data.current_page === 1) {
            this.data.list = []
          }
          this.setData({
            list: [
              ...this.data.list,
              ...currentData
            ],
            page: data.current_page,
            loadMore: data.current_page !== data.last_page,
            account_balance: data.account_balance,
            give_balance: data.give_balance,
            recharge_balance: data.recharge_balance,
          })
        }
      }
    })
  },
  toRecharge () {
    wx.navigateTo({
      url: '/pages/recharge/index'
    })
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.getList(1)
  },
  onReachBottom () {
    if (!this.data.loadMore) {
      return false
    }
    this.getList(this.data.page + 1)
  },
})
